package com.haierCamera.customapplication.ui.user.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;

import com.haierCamera.customapplication.Constants;
import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.repo.UserRepo;
import com.haierCamera.customapplication.databinding.HzklActivityResetNicknameBinding;
import com.haierCamera.customapplication.BaseActivity;
import com.haierCamera.customapplication.utils.EditTextChangeBoolean;
import com.haierCamera.customapplication.utils.SharedPreferencesUtils;
import com.haierCamera.customapplication.utils.android.HttpErrorProcess;

import javax.inject.Inject;

import dagger.Lazy;

public class HZKLUserNameActivity extends BaseActivity {
    private HzklActivityResetNicknameBinding binding;
    @Inject
    UserRepo repo;
    @Inject
    Lazy<HttpErrorProcess> errorProcessLazy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.hzkl_activity_reset_nickname);
        initData();
    }

    private void initData() {
        binding.registerBack.setOnClickListener(v -> finish());
        binding.nameBtn.setOnClickListener(v -> setName());
        buttonBoolean();
    }

    private void buttonBoolean() {
        new EditTextChangeBoolean().buttonEnable(binding.userName, enable -> binding.nameBtn.setEnabled(enable));
    }

  /*  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.hzkl_yes, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.hzkl_yes:
                setName();
                break;
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }*/

    private void setName() {
        String name = binding.userName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "请输入昵称", Toast.LENGTH_SHORT).show();
            return;
        }
        repo.setNickname(name).observe(this, objectResource -> {
            switch (objectResource.status) {
                case LOADING:
                    showLoadingDialog();
                    break;
                case ERROR:
                    dismissLoadingDialog();
                    //Toast.makeText(this, objectResource.errorMessage, Toast.LENGTH_SHORT).show();
                    errorProcessLazy.get().process(objectResource);
                    break;
                case SUCCESS:
                    dismissLoadingDialog();
                    Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                    SharedPreferencesUtils.saveString(Constants.SP_USER_PHONE, name);
                    finish();
                    break;
            }
        });
    }
}
